package 华为OD机试真题2023.进阶题;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ10_最大数字 {
    static TreeSet<String> tmpSet = new TreeSet<>(Comparator.reverseOrder());

    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/10.txt"));
        String s = in.nextLine();
        List<Integer> list = new ArrayList<>();
        int[] count = new int[10];
        for (int i = 0; i < s.length(); i++) {
            int now = s.charAt(i) - '0';
            if (count[now] < 2) {
                list.add(now);
                count[now]++;
            } else {
                int index1 = list.indexOf(now);
                int index2 = list.lastIndexOf(now);
                if (list.get(index1 + 1) > now) {
                    list.remove(index1);
                    list.add(now);
                    continue;
                }
                if (index2 != list.size() - 1 && list.get(index2 + 1) > now) {
                    list.remove(index2);
                    list.add(now);
                    continue;
                }
            }
        }
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            res.append(list.get(i));
        }
        System.out.println(res);
    }
}
